package com.ykdl.member.kid.regandlogin;

import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import net.wxxr.dataparse.model.DataParseCode;
import net.wxxr.dataparse.model.DataParseError;
import net.wxxr.dataparse.model.DataState;
import net.wxxr.http.interfaces.ITag;
import net.wxxr.http.request.HttpBaseRequest;
import net.wxxr.http.util.Constants;
import oauth.signpost.http.HttpParameters;

import org.json.JSONObject;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.View.OnClickListener;
import android.view.WindowManager.LayoutParams;
import android.view.inputmethod.EditorInfo;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ykdl.member.kid.R;
import com.ykdl.member.constant.KidConfig;
import com.ykdl.member.kid.beans.ResetPasswordBean;
import com.ykdl.member.kid.beans.TimeLeftBean;
import com.ykdl.member.kid.gears.BaseScreen;
import com.ykdl.member.kid.gears.HomeActivity;
import com.ykdl.member.kid.regandlogin.PerfectUserInfoActivity.profileUserInfoTag;
import com.ykdl.member.kid.util.StringUtils;
import com.ykdl.net.manager.TaskManager;
import com.ykdl.net.manager.Wxxr;

public class ForgetPwActivity extends BaseScreen  {
	private Button back_bt,next_bt;
	private EditText phone_number;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTopTitle("手机找回", 0, R.drawable.back_bg, new OnClickListener() {
			@Override
			public void onClick(View v) {
				
				finish();
			}
		}, R.string.turn_ok, 0, new OnClickListener() {
			@Override
			public void onClick(View v) {
				if (checkPhone()) {
					checkIsSend(phone_number.getText().toString());
				}
			}
		}, R.drawable.title_bg, false);
		setContent(R.layout.forgetpw_xml);
		findbyid();
	}
	public void findbyid() {
		phone_number = (EditText)findViewById(R.id.edit_phone_number);
		phone_number.setInputType(EditorInfo.TYPE_CLASS_PHONE);
	}
	
	public boolean checkPhone(){
		if (phone_number.getText().toString().length()==11) {
			if (phone_number.getText().toString().matches("[0-9]{1,}")) {
				return true;
			}else {
				Toast.makeText(mContext, "手机号不正确", 1).show();
				return false;
			}
		}else {
			Toast.makeText(mContext, "请输入正确的手机号码", 0).show();
			return false;
		}   
    }
	public void goRestPw (String phoneString) {
		showProgress("正在发送请稍等...");
		String  pageurl = KidConfig.REGISTER_BYPHONE;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("app_key", "hsh");
		parameterss.put("phone", phoneString);
		parameterss.put("client_id", Constants.CLIENT_ID);
		parameterss.put("client_secret", Constants.CLIENT_SECRET);
		parameterss.put("type", "password");
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss, null);
		 TaskManager.startHttpRequest(request, new resetpasswordTag(),  ResetPasswordBean.class);
		
	}
	class resetpasswordTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			if (t instanceof ResetPasswordBean) {
				finishProgress();
				ResetPasswordBean bean = (ResetPasswordBean)t;
				if (bean.getError()!=null) {
						Toast.makeText(mContext, bean.getDesc(), 1).show();
				}else {
					if (bean.getStatus()==0) {
						Toast.makeText(mContext, "成功", 1).show();
						Log.e("alan", "-------t:"+t.toString());
						gonext() ;
					}else {
						Toast.makeText(mContext, bean.getDesc(), 1).show();
					}
					
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			Toast.makeText(mContext, "参数有误失败", 1).show();
			return true;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}
	
	public void sendCode() {
		if (checkPhone()) {
			goRestPw(phone_number.getText().toString());
		}else {
			Toast.makeText(mContext, "手机号有误，请重新输入", 1).show();
		}
	}
	public void gonext() {
		
		Intent intent = new Intent(this,ForgetPwOneActivity.class);
		intent.putExtra("phone_number", phone_number.getText().toString());
		startActivity(intent);
		ForgetPwActivity.this.finish();
	}
	public void checkIsSend (String phoneString) {
		showProgress("正在发送请稍等...");
		String  pageurl = KidConfig.PHONE_CONFIRM_TIMELEFT;
		HttpParameters parameterss = new HttpParameters();
		parameterss.put("app_key", "hsh");
		parameterss.put("phone", phoneString);
		HttpBaseRequest request  =  Wxxr.getInstance().postBaseRequest(pageurl,parameterss, null);
		 TaskManager.startHttpRequest(request, new checkIsSendTag(),  TimeLeftBean.class);
		
	}
	class checkIsSendTag implements ITag {

		@Override
		public void refreshUI(Object t, DataState ds) {
			// TODO Auto-generated method stub
			if (t instanceof TimeLeftBean) {
				finishProgress();
				TimeLeftBean bean = (TimeLeftBean)t;
				if (bean.getTime_left()==0) {
					sendCode();
				}else {
						Toast.makeText(mContext, "请稍等"+bean.getTime_left()+"秒", 1).show();
				}
			}
		}

		@Override
		public boolean refreshError(DataParseError parseError) {
			// TODO Auto-generated method stub
			finishProgress();
			return true;
		}

		@Override
		public void refreshCancel() {
			// TODO Auto-generated method stub
			
		}
		
	}

}
